<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Exceptions and errors</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="tutorial-Types.xhtml" title="Types and declarations"/>
    <link rel="next" href="tutorial-Classes.xhtml" title="Classes"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Exceptions and errors" epub:type="subchapter" id="Tutorial---Exceptions-and-errors">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Exceptions and errors</h2>
          </div>
        </div>
      </div>
      <p>Kawa supports the exception framework and forms
from R6RS and R7RS.  See <a class="link" href="Exceptions.xhtml" title="Exception handling">Exceptions</a> for details.
</p>
      <h3 id="idm139667880349648">Native exception handling</h3>
      <p>You can also work with native Java exceptions at a low level.
</p>
      <p>The <code class="literal">primitive-throw</code> procedure throws a <code class="literal">Throwable</code> value.
It is implemented just like Java’s <code class="literal">throw</code>.
</p>
      <pre class="screen">(primitive-throw (java.lang.IndexOutOfBoundsException "bad index"))
</pre>
      <p>You can catch an exception with the <code class="literal">try-catch</code> syntax.  For example:
</p>
      <pre class="screen">(try-catch
  (do-a-bunch-of-stuff)
  (ex java.lang.Throwable
    (format #f "caught ~a~%~!" ex)
    (exit)))
</pre>
      <p>A <code class="literal">try-finally</code> does the obvious:
</p>
      <pre class="screen">(define (call-with-port port proc)
  (try-finally
   (proc port)
   (close-port port)))
</pre>
      <p>Both <code class="literal">try-catch</code> and <code class="literal">try-finally</code> are
expression forms that can return values, while the corresponding
Java forms are statements that cannot return values.
</p>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="tutorial-index.xhtml">Kawa Scheme Tutorial</a></p>
        <p>
        Previous: <a accesskey="p" href="tutorial-Types.xhtml">Types and declarations</a></p>
        <p>
        Next: <a accesskey="n" href="tutorial-Classes.xhtml">Classes</a></p>
      </div>
    </footer>
  </body>
</html>
